﻿using System.Linq;
using nRoute.Components.Composition;
using nRoute.Navigation.Mapping;
using nRoute.Samples.Netflix.Data;
using nRoute.Samples.Netflix.Data.Catalog;
using nRoute.Samples.Netflix.Views;
using nRoute.ViewModels;
using System.Data.Services.Client;

namespace nRoute.Samples.Netflix.ViewModels
{
    [DefineNavigationContent("Content/Netflix/Movies/", typeof(MoviesListingView))]
    [MapViewModel(typeof(MoviesListingView))]
    public class MoviesListingViewModel
        : DataListingViewModelBase<Title, NetflixDataContext>
    {
        [ResolveConstructor]
        public MoviesListingViewModel(NetflixDataContext context)
            : base(context) { }

        protected override DataServiceQuery<Title> CreateQuery(string searchText)
        {
            var _query = base.Context.Titles.IncludeTotalCount();
            if (!string.IsNullOrWhiteSpace(searchText)) _query = (DataServiceQuery<Title>)_query.Where((t) => t.Name.IndexOf(searchText) != -1);
            return (DataServiceQuery<Title>)_query.OrderBy((g) => g.Name);
        }
    }    
}